package com.geekaca.web.servlet;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.geekaca.web.pojo.Brand;
import com.geekaca.web.pojo.BrandType;
import com.geekaca.web.service.BrandService;
import com.geekaca.web.utils.Result;

import javax.servlet.ServletException;
import javax.servlet.ServletInputStream;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;

@WebServlet(urlPatterns = "/updateBrand")
public class UpdateBrandServlet extends HttpServlet {
    private BrandService brandService = new BrandService();

    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

       /* // 登录确认
        HttpSession session = req.getSession();
        Object loggedObj = session.getAttribute("logged");

        System.out.println("loggedobj: " + loggedObj);
        String logged = loggedObj.toString();
        System.out.println("logged: " + logged);

        if (!logged.equals("1")) {
            resp.setHeader("Content-type", "text/html; charset=utf-8");
            PrintWriter writer = resp.getWriter();
            writer.write("<h1>非法请求，请先登录</h1>");
            return;
        }*/

        // 更新
        ServletInputStream inputStream = req.getInputStream();
        BufferedReader br = new BufferedReader(new InputStreamReader(inputStream, "utf-8"));

        String s = br.readLine();
        System.out.println(s);
        JSONObject jsonObject = JSON.parseObject(s, JSONObject.class);

        Integer id = (Integer) jsonObject.get("id");
        String brandName = (String) jsonObject.get("brandName");
        String companyName = (String) jsonObject.get("companyName");

        Object status = jsonObject.get("status");
        Integer statusInt = Integer.valueOf((String) status);

        String description = (String) jsonObject.get("description");

        Integer ordered = (Integer) jsonObject.get("ordered");

        Integer type_id = (Integer) jsonObject.get("typeId");

        System.out.println("updatebrand servlet...");


        BrandType brandType;

//        Integer type_id = 1;
        switch (type_id) {
            case 1:
                brandType = new BrandType(1, 1, "食品");
                break;
            case 2:
                brandType = new BrandType(2, 2, "数码");
                break;
            case 3:
                brandType = new BrandType(3, 3, "文化");
                break;
            default:
                brandType = new BrandType();
        }

        Brand brand = new Brand(id, brandName, companyName, ordered, description, statusInt, type_id, brandType);

        int updated = brandService.updateBrand(brand);
        Result result = new Result();
        PrintWriter writer = resp.getWriter();
        if (updated > 0) {
            result.setCode(200);
            result.setMsg("更新成功");
            String s1 = JSON.toJSONString(result);
            writer.write(s1);
            writer.flush();
        } else {
            result.setCode(400);
            result.setMsg("更新失败");
            String s1 = JSON.toJSONString(result);
            writer.write(s1);
            writer.flush();
        }
    }
}
